package shoppingcart.fragment.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.xfengshopping.R;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import shoppingcart.fragment.model.ShopList;

public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ViewHolder> {
    private List<ShopList> list;
    private boolean[] isSelected;
    private final DecimalFormat priceFormat = new DecimalFormat("0.00");
    private OnItemActionListener itemActionListener;

    public ShopAdapter(List<ShopList> list) {
        this.list = new ArrayList<>(list);
        this.isSelected = new boolean[list.size()];
    }

    public interface OnItemActionListener {
        void onItemClick(int position);
        void onQuantityIncreased(int position, int newQuantity);
        void onQuantityDecreased(int position, int newQuantity);
        void onSelectionChanged(int position, boolean isSelected);
    }

    public void setOnItemActionListener(OnItemActionListener listener) {
        this.itemActionListener = listener;
    }

    public double getSelectedSum() {
        double sum = 0.0;
        for (int i = 0; i < list.size(); i++) {
            if (isSelected[i]) {
                sum += list.get(i).getGoodsCount() * list.get(i).getSellingPrice();
            }
        }
        return sum;
    }

    public int getSelectedItemCount() {
        int count = 0;
        for (boolean selected : isSelected) {
            if (selected) count++;
        }
        return count;
    }

    public void selectAllItems(boolean select) {
        for (int i = 0; i < isSelected.length; i++) {
            isSelected[i] = select;
        }
        notifyDataSetChanged();
        if (itemActionListener != null) {
            for (int i = 0; i < list.size(); i++) {
                itemActionListener.onSelectionChanged(i, select);
            }
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView photo, photoAdd, photoDown;
        TextView textName, price, num;
        CheckBox radio;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            photo = itemView.findViewById(R.id.photo);
            photoAdd = itemView.findViewById(R.id.photo_add);
            photoDown = itemView.findViewById(R.id.photo_down);
            textName = itemView.findViewById(R.id.text1);
            price = itemView.findViewById(R.id.price);
            num = itemView.findViewById(R.id.num);
            radio = itemView.findViewById(R.id.radio);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.cart_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ShopList shop = list.get(position);

        holder.textName.setText(shop.getGoodsName());
        holder.price.setText(priceFormat.format(shop.getSellingPrice()));
        holder.num.setText(String.valueOf(shop.getGoodsCount()));

        Glide.with(holder.itemView.getContext())
                .load(shop.getGoodsCoverImg())
                .placeholder(R.drawable.ic_placeholder)
                .error(R.drawable.ic_error)
                .into(holder.photo);

        holder.radio.setChecked(isSelected[position]);
        holder.radio.setOnCheckedChangeListener((buttonView, isChecked) -> {
            isSelected[position] = isChecked;
            if (itemActionListener != null) {
                itemActionListener.onSelectionChanged(position, isChecked);
            }
        });

        holder.photoAdd.setOnClickListener(v -> {
            int newQuantity = shop.getGoodsCount() + 1;
            if (itemActionListener != null) {
                itemActionListener.onQuantityIncreased(position, newQuantity);
            }
        });

        holder.photoDown.setOnClickListener(v -> {
            int currentQuantity = shop.getGoodsCount();
            if (currentQuantity > 1) {
                int newQuantity = currentQuantity - 1;
                if (itemActionListener != null) {
                    itemActionListener.onQuantityDecreased(position, newQuantity);
                }
            }
        });

        holder.itemView.setOnClickListener(v -> {
            if (itemActionListener != null) {
                itemActionListener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void updateData(List<ShopList> newList) {
        this.list = new ArrayList<>(newList);
        this.isSelected = new boolean[newList.size()];
        notifyDataSetChanged();
    }

    public ShopList getItem(int position) {
        return list.get(position);
    }
    public boolean isSelected(int position) {
        if (position >= 0 && position < isSelected.length) {
            return isSelected[position];
        }
        return false;
    }
}